import { Component, TemplateRef, Input, Output, EventEmitter } from '@angular/core';

import { NzNotificationService } from 'ng-zorro-antd/notification';


interface Point {
  x: number; // 像素X坐标
  y: number; // 像素Y坐标
}



@Component({
  selector: 'app-demo01',
  templateUrl: './demo01.component.html',
  styleUrls: ['./demo01.component.less']
})
export class Demo01Component {
  /**
   * event 自定义事件名
   */
  @Output() event = new EventEmitter();


  /**
   * @Input() 父组件传入
   */
  @Input() btnText: any = '确定';
  @Input() nzDuration: any = 3000;
  @Input() nzClass: any = 'tempClass';
  @Input() nzCloseIcon: any = 'close-circle';

  constructor(private notification: NzNotificationService) { }

  // 
  childenTest = false;


  // ngOnInit() {
  //   console.log(this.btnText, 'this.btnText');

  // }

  createBasicNotification(template01: TemplateRef<{}>) {
    this.notification.template(template01, { nzDuration: this.nzDuration, nzClass: this.nzClass, nzCloseIcon: this.nzCloseIcon });
  }

  handleNotification(notification01: any) {
    console.log(this.btnText, 'this.btnText');

    // console.log(333);
    this.childenTest = true;

    this.event.emit(this.childenTest);

    notification01.close()
  }

  handlerChild() {
    alert('触发子组件事件~')

  }


  firstPoint: Point | null = null;       // 第一个确认点
  secondPoint: Point | null = null;      // 第二个确认点
  tempPoint: Point | null = null;        // 鼠标移动时的临时点
  currentDistance = 0;                   // 当前距离（厘米）
  showDistance = false;                  // 是否显示距离
  private pixelsPerCm = 0;               // 每厘米像素数

  ngOnInit() {
    // 初始化屏幕每厘米像素数（基于DPI）
    const dpi = this.getScreenDPI();
    this.pixelsPerCm = dpi / 2.54; // 1英寸 = 2.54厘米
  }

  // 获取屏幕DPI（结合设备像素比）
  private getScreenDPI(): number {
    const dpr = window.devicePixelRatio || 1;
    return 96 * dpr; // 系统默认DPI为96，乘以设备像素比
  }

  // 点击事件：第一次点击记录点1，第二次记录点2
  onPointClick(event: MouseEvent): void {
    const currentPoint: Point = {
      x: event.offsetX,
      y: event.offsetY
    };

    if (!this.firstPoint) {
      // 第一次点击：记录点1，开始显示距离
      this.firstPoint = currentPoint;
      this.showDistance = true;
    } else if (!this.secondPoint) {
      // 第二次点击：记录点2，计算最终距离
      this.secondPoint = currentPoint;
      this.tempPoint = null; // 清空临时点
      const pixelDistance = this.calculatePixelDistance(this.firstPoint, this.secondPoint);
      this.currentDistance = pixelDistance / this.pixelsPerCm;
    }
  }

  // 鼠标移动时：实时更新临时点并计算距离（仅在点1已确认且点2未确认时）
  onMouseMove(event: MouseEvent): void {
    if (this.firstPoint && !this.secondPoint) {
      this.tempPoint = {
        x: event.offsetX,
        y: event.offsetY
      };
      // 计算临时距离（像素转厘米）
      const pixelDistance = this.calculatePixelDistance(this.firstPoint, this.tempPoint);
      this.currentDistance = pixelDistance / this.pixelsPerCm;
    }
  }

  // 计算两点像素距离
  private calculatePixelDistance(p1: Point, p2: Point): number {
    const dx = p2.x - p1.x;
    const dy = p2.y - p1.y;
    return Math.sqrt(dx * dx + dy * dy);
  }

  // 重置测量
  reset(): void {
    this.firstPoint = null;
    this.secondPoint = null;
    this.tempPoint = null;
    this.currentDistance = 0;
    this.showDistance = false;
  }

}
